1

Tomcat排雷笔记

1. 出现场景
eclipse发布web应用至tomcat,默认方式下访问该项目是需要带项目名称的,例http://localhost:8080/myapp/
现在需要改成这样访问http://localhost
修改tomcat的server.xml文件,增加如下配置<Context path="" docBase="myapp" reloadable="false"/>,修改后host部分如下:

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

       <Context path="" docBase="myapp" reloadable="false"/>

      </Host>

此时每次启动tomcat观察日志都会发现应用加载了两次,什么原因呢?

2. 问题原因

虚拟目录引起的问题,我们在Host标签里配置了appBase="webapps",tomcat加载一次应用。在里配置了一次docBase,tomcat又去加载一次应用。

3. 解决办法

将appBase="webapps"置为appBase="",docBase="myapp"置为docBase="webapps/myapp"。配置如下:

<Host name="localhost"  appBase=""
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

       <Context path="" docBase="webapps/myapp" reloadable="false"/>

      </Host>

说明:如果你的项目中的链接或者图片写的都是绝对路径的话,那么带有项目名称的绝对路径是不可以使用的。


dahong
32 声望0 粉丝